/* $Id: UserReqDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.UserReqDAO;
import ae.pcfc.etks.online.common.domain.ClientPersonReq;
import ae.pcfc.etks.online.common.domain.UserReq;

@Repository
public class UserReqDAOImpl extends BaseDAO implements UserReqDAO {

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserReqDAO#add(ae.pcfc.etks.online.common.domain.UserReq)
	 * 
	 */
	public void add(UserReq userReq) {

		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.save(userReq);
	}

	/**
	 * findByApplicationId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserReqDAO#findByApplicationId(java.lang.String)
	 * 
	 */
	public UserReq findByApplicationId(String applicationId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(UserReq.class, "userReq");
		criteria.add(Restrictions.eq("userReq.applicationId", applicationId));

		@SuppressWarnings("unchecked")
		List<UserReq> list = criteria.list();

		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * findByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserReqDAO#findByUserName(java.lang.String)
	 * 
	 */
	public UserReq findByUserName(String userName) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(UserReq.class, "userReq");
		criteria.add(Restrictions.eq("userReq.UReqUsername", userName));

		@SuppressWarnings("unchecked")
		List<UserReq> list = criteria.list();

		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * findByEmail
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserReqDAO#findByEmail(java.lang.String)
	 * 
	 */
	public UserReq findByEmail(String email) {

		Criteria criteria = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createCriteria(UserReq.class, "userReq");
		criteria.add(Restrictions.eq("userReq.UReqEmail", email));

		@SuppressWarnings("unchecked")
		List<UserReq> list = criteria.list();

		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * validateByPropertyName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserReqDAO#validateByPropertyName(java.lang.String, java.lang.String)
	 * 
	 */
	public boolean validateByPropertyName(String strPropertyName,
			String strPropertyValue) {
		org.hibernate.Query query = getHibernateTemplate()
				.getSessionFactory()
				.getCurrentSession()
				.createQuery(
						" from UserReq userreq , AppMetaData appMeta where userreq.applicationId =  appMeta.applicationId and appMeta.wfStatus != :status and lower(userreq."
								+ strPropertyName + ") = lower(:propvalue)");
		query.setParameter("status", ConstantsCommon.REJECT);
		query.setParameter("propvalue", strPropertyValue);
		query.list();
		List<ClientPersonReq> list = query.list();
		if (list != null && list.size() > 0)
			return false;
		else
			return true;
	}
}
